package com.ts.controller.achievement;

import com.ts.constant.AchievementConstant;
import com.ts.constant.JwtClaimsConstant;
import com.ts.dto.HonorsAwardDTO;
import com.ts.result.Result;
import com.ts.service.achievement.TeacherHonorsService;
import com.ts.service.achievement.TsAchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 荣誉获奖相关接口 - 荣誉获奖成果的提交，查询
 */
@RestController
@RequestMapping("/achievement/honors")
@Tag(name = "荣誉获奖相关接口", description = "荣誉获奖的提交和查询功能")
@Slf4j
public class TeacherHonorsController {

    @Autowired
    private TeacherHonorsService teacherHonorsService;
    @Autowired
    private TsAchievementService tsAchievementService;


    /**
     * 提交一个荣誉获奖成果
     * @param honorsAwardDTO
     * @return
     */
    @Operation(summary = "提交", description = "提交荣誉获奖成果")
    @PostMapping()
    public Result saveHonorsAward(@RequestBody HonorsAwardDTO honorsAwardDTO) {
        log.info("honorsAwardDTO:{}", honorsAwardDTO);

        // 先保存荣誉获奖信息
        teacherHonorsService.save(honorsAwardDTO);

        // 设置成就相关参数
        String type = AchievementConstant.HONORARY_AWARD; // 使用正确的成就类型常量
        String achievename = honorsAwardDTO.getHonorsName(); // 从DTO获取荣誉名称
        String description = honorsAwardDTO.getRemarks() != null ? honorsAwardDTO.getRemarks() : "荣誉获奖成果"; // 使用备注或默认描述
        Integer level = 2; // 可以根据实际情况设置级别
        Integer createdBy = 8; // 从安全上下文获取当前用户ID
        Long softId = honorsAwardDTO.getId(); // 使用实例对象而不是类名

        // 添加成就记录
        tsAchievementService.addAchievement(achievename, description, type, level, createdBy, softId);

        return Result.success();
    }
    @Operation(summary = "更新", description = "更新荣誉获奖成果")
    @PutMapping()
    public Result updateHonorsAward(@RequestBody HonorsAwardDTO honorsAwardDTO) {
        log.info("update honorsAwardDTO:{}", honorsAwardDTO);
        teacherHonorsService.update(honorsAwardDTO);
        return Result.success();
    }

    @Operation(summary = "删除", description = "删除荣誉获奖成果")
    @DeleteMapping("/{id}")
    public Result deleteHonorsAward(@PathVariable Long id) {
        log.info("delete honors id:{}", id);
        teacherHonorsService.delete(id);
        return Result.success();
    }

    @Operation(summary = "查询单个", description = "根据ID查询荣誉获奖成果")
    @GetMapping("/{id}")
    public Result<HonorsAwardDTO> getHonorsAwardById(@PathVariable Long id) {
        log.info("get honors by id:{}", id);
        HonorsAwardDTO honorsAwardDTO = teacherHonorsService.getById(id);
        return Result.success(honorsAwardDTO);
    }

    @Operation(summary = "查询全部", description = "查询所有荣誉获奖成果")
    @GetMapping("/list")
    public Result<List<HonorsAwardDTO>> listAllHonorsAwards() {
        log.info("list all honors");
        List<HonorsAwardDTO> list = teacherHonorsService.listAll();
        return Result.success(list);
    }
}
